import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:get/get.dart';
import 'package:kq_flutter_core_widget/utils/toast_util.dart';
import 'package:kq_flutter_tv_widgets/widgets/button/kq_small_button.dart';
import 'package:kq_flutter_tv_widgets/widgets/stackView/stack_view_child_widget.dart';
import 'package:kq_flutter_core_widget/widgets/stackView/stack_view.dart';
import 'package:kq_flutter_tv_widgets/widgets/stackView/stack_view_widget.dart';

///一体机导航框架demo
class TerminalNavDemo extends StatefulWidget {
  const TerminalNavDemo({super.key});

  @override
  State<StatefulWidget> createState() => TerminalNavDemoState();
}

class TerminalNavDemoState extends State<TerminalNavDemo> {
  StackViewState? state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StackViewWidget(
        init: (state) {
          return TestParentPage1(state: state);
        },
      ),
    );
  }
}

class TestParentPage1 extends StackViewChildWidget {
  const TestParentPage1({super.key, required super.state});

  @override
  Widget buildChildPage(BuildContext context) {
    return Column(
      children: [
        Expanded(
          child: Container(
            color: Colors.purple,
            alignment: Alignment.center,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                const Text("我是首页"),
                SizedBox(
                  height: 20.r,
                ),
                SizedBox(
                  width: 100.r,
                  child: KqSmallButton(
                    title: "打开新页面",
                    onTap: (disabled) {
                      open(
                        TestParentPage2(state: state),
                        arguments: {"testOpen": "我是首页传过来的测试数据！"},
                        callback: (result) {
                          KqToast.showNormal("我是返回值：$result");
                        },
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}

class TestParentPage2 extends StackViewChildWidget {
  const TestParentPage2({super.key, required super.state});

  @override
  Widget buildChildPage(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: Column(
            children: [
              Expanded(
                child: Container(
                  color: Colors.amber,
                  alignment: Alignment.center,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text("我是TestPage1，上一个界面传值来了：${getArguments()}"),
                      SizedBox(
                        height: 20.r,
                      ),
                      SizedBox(
                        width: 100.r,
                        child: KqSmallButton(
                          title: "我主动关闭",
                          onTap: (disabled) {
                            back(result: {"testBack": "我是返回值"});
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
        Expanded(child: StackViewWidget(
          init: (state) {
            return TestChildPage1(
              state: state,
            );
          },
        )),
      ],
    );
  }
}

class TestChildPage1 extends StackViewChildWidget {
  const TestChildPage1({super.key, required super.state});

  @override
  Widget buildChildPage(BuildContext context) {
    return Column(
      children: [
        Expanded(
            child: Container(
          color: Colors.blueAccent,
          alignment: Alignment.center,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const Text("我是子页面的首页"),
              SizedBox(
                height: 20.r,
              ),
              SizedBox(
                width: 100.r,
                child: KqSmallButton(
                  title: "打开新页面",
                  onTap: (disabled) {
                    open(TestChildPage2(state: state));
                  },
                ),
              ),
            ],
          ),
        )),
      ],
    );
  }
}

class TestChildPage2 extends StackViewChildWidget {
  const TestChildPage2({super.key, required super.state});

  @override
  Widget buildChildPage(BuildContext context) {
    return Column(
      children: [
        Expanded(
            child: Container(
          color: Colors.red,
          alignment: Alignment.center,
          child: const Text("我是子页面"),
        )),
      ],
    );
  }
}
